package com.zp.self;

import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@EnableApolloConfig
@SpringBootApplication
public class ApolloApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApolloApplication.class, args);
    }

    @RestController
    public class ConfigController {

        @Resource
        private ConfigProperties configProperties;

        @Value("${name}")
        private String name;

        @GetMapping("/name")
        public String getName() {
            return configProperties.getName();
        }

        @GetMapping("/mysql")
        public Map<Object, Object> getMySQLProperties() {
            // JDK9中的新特性，快速创建只读集合。
            HashMap<Object, Object> map = new HashMap<>();
            map.put("host", configProperties.getMysqlHost());
            map.put("port", configProperties.getMysqlPort());
            map.put("username", configProperties.getMysqlUsername());
            map.put("password", configProperties.getMysqlPassword());
            return map;
        }

    }
}